//
//  EmptyStateView.m
//  ChatDemo-UI2.0
//
//  Created by syun on 16/10/24.
//  Copyright © 2016年 syun. All rights reserved.
//

#import "EmptyStateView.h"

@implementation EmptyStateView

- (instancetype)initWithFrame:(CGRect)frame andBackImage:(NSString *)imageStr andBottomStr:(NSString *)bottomStr
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.userInteractionEnabled = YES;
        
        _backImage =[[UIImageView alloc]initWithFrame:CGRectMake((frame.size.width-95)/2, 0, 95, frame.size.height)];
        _backImage.backgroundColor=[UIColor clearColor];
        _backImage.image=[UIImage imageNamed:imageStr];
        _backImage.userInteractionEnabled = YES;
        [self addSubview:_backImage];
        
        _bottomLable = [[UILabel alloc]initWithFrame:CGRectMake(0, _backImage.bottom, self.width, 17)];
        [_bottomLable setFont:[UIFont systemFontOfSize:17]];
        [_bottomLable setTextColor:[UIColor colorWithHexString:@"#979797"]];
        _bottomLable.text = bottomStr;
        _bottomLable.textAlignment = NSTextAlignmentCenter;
        _bottomLable.userInteractionEnabled = YES;
        [self addSubview:_bottomLable];
        
        
        self.hidden = YES;
    }
    return self;
}
- (instancetype)initNoWifiWithFrame:(CGRect)frame andBackImage:(NSString *)imageStr andBottomStr:(NSString *)bottomStr
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.userInteractionEnabled = YES;
        
        _backImage =[[UIImageView alloc]initWithFrame:CGRectMake((frame.size.width-95)/2, 0, 95, 110)];
        _backImage.backgroundColor=[UIColor clearColor];
        _backImage.image=[UIImage imageNamed:imageStr];
        _backImage.userInteractionEnabled = YES;
        [self addSubview:_backImage];
        
        _bottomLable = [[UILabel alloc]initWithFrame:CGRectMake(0, _backImage.bottom, self.width, 17)];
        [_bottomLable setFont:[UIFont systemFontOfSize:17]];
        [_bottomLable setTextColor:[UIColor colorWithHexString:@"#979797"]];
        _bottomLable.text = bottomStr;
        _bottomLable.textAlignment = NSTextAlignmentCenter;
        _bottomLable.userInteractionEnabled = YES;
        [self addSubview:_bottomLable];
        
        
        _reloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _reloadBtn.frame = CGRectMake((frame.size.width-100)/2, _bottomLable.frame.size.height+_bottomLable.frame.origin.y + 20, 100, 28);
        [_reloadBtn setImage:[UIImage imageNamed:@"reloadImage"] forState:UIControlStateNormal];
        [_reloadBtn addTarget:self action:@selector(reloadDataAction:) forControlEvents:UIControlEventTouchUpInside];
        _reloadBtn.userInteractionEnabled = YES;
        _reloadBtn.hidden = YES;
        
        [self addSubview:_reloadBtn];
        
        self.hidden = YES;
    }
    return self;
}

- (void)reloadDataAction:(UIButton *)sender
{
    if (_delegate && [_delegate respondsToSelector:@selector(reloadCurrentData)]) {
        
        [_delegate reloadCurrentData];
    }
    
}

- (void)showEmptyImage
{
    self.hidden = NO;
    
    if ([_bottomLable.text isEqualToString:@"网络出错啦,点击重新加载"]) {
        
        _reloadBtn.hidden = NO;
    }
}

- (void)hideEmptyImage
{
    
    self.hidden = YES;
    
    if ([_bottomLable.text isEqualToString:@"网络出错啦,点击重新加载"]) {
        
        _reloadBtn.hidden = YES;
    }
}
@end
